﻿@using System.Globalization;
@using System.Diagnostics.CodeAnalysis
@inherits BasicComponent

@UI.BuildDropdown(options)

@code {
    string current = CultureInfo.DefaultThreadCurrentUICulture?.Name ?? "";
    [Inject, NotNull] IProtectedLocalStorage? Storage { get; set; }
    [Parameter, NotNull] public CultureOptions? Options { get; set; }
    [Inject, NotNull] public ILanguageService? Language { get; set; }
    DropdownOptions options = new();

    protected override void OnInitialized()
    {
        base.OnInitialized();
        options.Content =@<span style="display:flex">
        <MIcon IconName="svg-language"
               ClassName="nav-menu-icon" />
    </span>
    ;
        options.Actions = Options.SupportedCulture.Select(l => new ActionInfo()
            {
                Label = l.Name,
                OnClick = EventCallback.Factory.Create(this, () =>
            {
                ToggleLang(l.Culture);
            })
            }).ToList();
    }
        
    void ToggleLang(string lang)
    {
        if (lang != CultureInfo.DefaultThreadCurrentUICulture?.Name)
        {
            var culture = CultureInfo.GetCultureInfo(lang);
            App.AppLanguage = lang;
            Storage.SetAsync(ConstraintString.APP_STORE_KEY, App);
            Language.SetLanguage(lang);
            Navigator.NavigateTo(Navigator.Uri);
        }
    }
}